//
//  LBCityButton.m
//  UISearchViewController
//
//  Created by LINAICAI on 2017/3/30.
//  Copyright © 2017年 LINAICAI. All rights reserved.
//

#import "LBCityButton.h"
#import <Masonry/Masonry.h>

static const CGFloat limit = 70.0;
@interface LBCityButton()
@property(nonatomic , strong)UILabel *cityLabel;
@property(nonatomic , strong)UIImageView *arrowImageView;
@property(nonatomic , copy)void(^click)(NSString *cityName);
@end
@implementation LBCityButton
- (UIImageView *)arrowImageView{
    if (!_arrowImageView) {
        _arrowImageView = [UIImageView new];
        NSString *bundlePath = [[NSBundle mainBundle].resourcePath stringByAppendingPathComponent:@"city.bundle"];
        NSBundle *bundle = [NSBundle bundleWithPath:bundlePath];
        NSString *plistPath = [bundle pathForResource:@"arrow@3x" ofType:@"png"];
        _arrowImageView.image = [[UIImage imageWithContentsOfFile:plistPath]imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
        _arrowImageView.contentMode = UIViewContentModeCenter;
    }
    return _arrowImageView;
}
- (UILabel *)cityLabel{
    if (!_cityLabel) {
        _cityLabel = [UILabel new];
        _cityLabel.textColor = [UIColor whiteColor];
        _cityLabel.font = [UIFont systemFontOfSize:13];
        _cityLabel.numberOfLines = 0;
    }
    return _cityLabel;
}
- (instancetype)initWithTitle:(NSString *)title click:(void(^)(NSString *cityName))click{
    self = [super init];
    if (self) {
        [self addSubview:self.cityLabel];
        [self addSubview:self.arrowImageView];
        self.userInteractionEnabled = YES;
        [self addGestureRecognizer:[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(TapEvent:)]];
        self.click = click;
        self.cityLabel.text = title;
       
        
       
        [self.cityLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.bottom.mas_equalTo(self);
            make.width.mas_lessThanOrEqualTo(80);
        }];
        [self.arrowImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.cityLabel.mas_right).offset(5);
            make.centerY.equalTo(self);
            make.width.height.equalTo(@10);
        }];
        
        [self mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.cityLabel);
            make.right.equalTo(self.arrowImageView);
            make.top.equalTo(self.cityLabel);
            make.bottom.equalTo(self.cityLabel);
        }];
        [self layoutIfNeeded];
    }
    return self;
}
- (void)TapEvent:(id)sender{
    if (self.click) {
        self.click(self.cityLabel.text);
    }
}
- (CGFloat)withWithText:(NSString *)text font:(UIFont *)font{
    CGSize size = [text sizeWithAttributes: @{NSFontAttributeName: font}];
    CGFloat with = size.width + (text.length/2)*0.5;
    return (with-limit)>=0?limit:with;
}
- (void)layoutSubviews{
    [super layoutSubviews];
    
}
- (void)updateCityName:(NSString *)cityName{
    self.cityLabel.text = cityName;
//    [self.cityLabel mas_updateConstraints:^(MASConstraintMaker *make) {
//        make.width.equalTo([NSNumber numberWithFloat:[self withWithText:self.cityLabel.text font:self.cityLabel.font]]);
//    }];
    
}

@end
